home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Common / include / dsutil.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  5.8 KB  |  172 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DSUtil.h
  3. //
  4. // Desc: 
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef DSUTIL_H
  9. #define DSUTIL_H
  10.  
  11. #include <windows.h>
  12. #include <mmsystem.h>
  13. #include <mmreg.h>
  14. #include <dsound.h>
  15.  
  16.  
  17.  
  18.  
  19. //-----------------------------------------------------------------------------
  20. // Classes used by this header
  21. //-----------------------------------------------------------------------------
  22. class CSoundManager;
  23. class CSound;
  24. class CStreamingSound;
  25. class CWaveFile;
  26.  
  27.  
  28.  
  29.  
  30. //-----------------------------------------------------------------------------
  31. // Typing macros 
  32. //-----------------------------------------------------------------------------
  33. #define WAVEFILE_READ   1
  34. #define WAVEFILE_WRITE  2
  35.  
  36. #define DSUtil_StopSound(s)         { if(s) s->Stop(); }
  37. #define DSUtil_PlaySound(s)         { if(s) s->Play( 0, 0 ); }
  38. #define DSUtil_PlaySoundLooping(s)  { if(s) s->Play( 0, DSBPLAY_LOOPING ); }
  39.  
  40.  
  41.  
  42.  
  43. //-----------------------------------------------------------------------------
  44. // Name: class CSoundManager
  45. // Desc: 
  46. //-----------------------------------------------------------------------------
  47. class CSoundManager
  48. {
  49. protected:
  50.     LPDIRECTSOUND8 m_pDS;
  51.  
  52. public:
  53.     CSoundManager();
  54.     ~CSoundManager();
  55.  
  56.     HRESULT Initialize( HWND hWnd, DWORD dwCoopLevel, DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
  57.     inline  LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }
  58.     HRESULT SetPrimaryBufferFormat( DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
  59.     HRESULT Get3DListenerInterface( LPDIRECTSOUND3DLISTENER* ppDSListener );
  60.  
  61.     HRESULT Create( CSound** ppSound, LPTSTR strWaveFileName, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
  62.     HRESULT CreateFromMemory( CSound** ppSound, BYTE* pbData, ULONG ulDataSize, LPWAVEFORMATEX pwfx, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
  63.     HRESULT CreateStreaming( CStreamingSound** ppStreamingSound, LPTSTR strWaveFileName, DWORD dwCreationFlags, GUID guid3DAlgorithm, DWORD dwNotifyCount, DWORD dwNotifySize, HANDLE hNotifyEvent );
  64. };
  65.  
  66.  
  67.  
  68.  
  69. //-----------------------------------------------------------------------------
  70. // Name: class CSound
  71. // Desc: Encapsulates functionality of a DirectSound buffer.
  72. //-----------------------------------------------------------------------------
  73. class CSound
  74. {
  75. protected:
  76.     LPDIRECTSOUNDBUFFER* m_apDSBuffer;
  77.     DWORD                m_dwDSBufferSize;
  78.     CWaveFile*           m_pWaveFile;
  79.     DWORD                m_dwNumBuffers;
  80.  
  81.     HRESULT RestoreBuffer( LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored );
  82.  
  83. public:
  84.     CSound( LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, CWaveFile* pWaveFile );
  85.     virtual ~CSound();
  86.  
  87.     HRESULT Get3DBufferInterface( DWORD dwIndex, LPDIRECTSOUND3DBUFFER* ppDS3DBuffer );
  88.     HRESULT FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
  89.     LPDIRECTSOUNDBUFFER GetFreeBuffer();
  90.     LPDIRECTSOUNDBUFFER GetBuffer( DWORD dwIndex );
  91.  
  92.     HRESULT Play( DWORD dwPriority = 0, DWORD dwFlags = 0 );
  93.     HRESULT Stop();
  94.     HRESULT Reset();
  95.     BOOL    IsSoundPlaying();
  96. };
  97.  
  98.  
  99.  
  100.  
  101. //-----------------------------------------------------------------------------
  102. // Name: class CStreamingSound
  103. // Desc: Encapsulates functionality to play a wave file with DirectSound.  
  104. //       The Create() method loads a chunk of wave file into the buffer, 
  105. //       and as sound plays more is written to the buffer by calling 
  106. //       HandleWaveStreamNotification() whenever hNotifyEvent is signaled.
  107. //-----------------------------------------------------------------------------
  108. class CStreamingSound : public CSound
  109. {
  110. protected:
  111.     DWORD m_dwLastPlayPos;
  112.     DWORD m_dwPlayProgress;
  113.     DWORD m_dwNotifySize;
  114.     DWORD m_dwNextWriteOffset;
  115.     BOOL  m_bFillNextNotificationWithSilence;
  116.  
  117. public:
  118.     CStreamingSound( LPDIRECTSOUNDBUFFER pDSBuffer, DWORD dwDSBufferSize, CWaveFile* pWaveFile, DWORD dwNotifySize );
  119.     ~CStreamingSound();
  120.  
  121.     HRESULT HandleWaveStreamNotification( BOOL bLoopedPlay );
  122.     HRESULT Reset();
  123. };
  124.  
  125.  
  126.  
  127.  
  128. //-----------------------------------------------------------------------------
  129. // Name: class CWaveFile
  130. // Desc: Encapsulates reading or writing sound data to or from a wave file
  131. //-----------------------------------------------------------------------------
  132. class CWaveFile
  133. {
  134. public:
  135.     WAVEFORMATEX* m_pwfx;        // Pointer to WAVEFORMATEX structure
  136.     HMMIO         m_hmmio;       // MM I/O handle for the WAVE
  137.     MMCKINFO      m_ck;          // Multimedia RIFF chunk
  138.     MMCKINFO      m_ckRiff;      // Use in opening a WAVE file
  139.     DWORD         m_dwSize;      // The size of the wave file
  140.     MMIOINFO      m_mmioinfoOut;
  141.     DWORD         m_dwFlags;
  142.     BOOL          m_bIsReadingFromMemory;
  143.     BYTE*         m_pbData;
  144.     BYTE*         m_pbDataCur;
  145.     ULONG         m_ulDataSize;
  146.     CHAR*         m_pResourceBuffer;
  147.  
  148. protected:
  149.     HRESULT ReadMMIO();
  150.     HRESULT WriteMMIO( WAVEFORMATEX *pwfxDest );
  151.  
  152. public:
  153.     CWaveFile();
  154.     ~CWaveFile();
  155.  
  156.     HRESULT Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
  157.     HRESULT OpenFromMemory( BYTE* pbData, ULONG ulDataSize, WAVEFORMATEX* pwfx, DWORD dwFlags );
  158.     HRESULT Close();
  159.  
  160.     HRESULT Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
  161.     HRESULT Write( UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );
  162.  
  163.     DWORD   GetSize();
  164.     HRESULT ResetFile();
  165.     WAVEFORMATEX* GetFormat() { return m_pwfx; };
  166. };
  167.  
  168.  
  169.  
  170.  
  171. #endif // DSUTIL_H
  172.